package ncit.bluetooth.settings;

import ncit.bluetooth.control.R;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.view.View.OnClickListener;;

//TODO: construct a singleton class that loads and saves the settings presented here.
//SAVE all the settings in that class.
public class BluetoothControlSettingsActivity extends Activity implements OnItemSelectedListener{

	public static final String PREFS_NAME = "SettingsFile";
	private BluetoothControlSettings controlSettings = null;
	
	
	@Override
	public void onCreate(Bundle instance)
	{
		super.onCreate(instance);
		setContentView(R.layout.settings);
		
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

		controlSettings = new BluetoothControlSettings(settings);
		controlSettings.loadSettings();
		
		//if(Commands.integerSettings != null) integerSettings = Commands.integerSettings;//TODO: SAVE ON DISK
		
		
		//if(Commands.stringSettings != null) stringSettings = Commands.stringSettings;//TODO: SAVE ON DISK
		
		//BluetoothControlSettings.DEVICE_TYPE = Commands.DeviceType;
		
		Spinner spinner = (Spinner) findViewById(R.id.spinner);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.device_type_select, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(this);
		spinner.setSelection(BluetoothControlSettings.DEVICE_TYPE);


		Button advanceSettings = (Button) findViewById(R.id.advance_settings_button);
		advanceSettings.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Intent i = null;

				if(BluetoothControlSettings.DEVICE_TYPE == 0)
					i = new Intent(getApplicationContext(), AdvanceSettingsLego.class);
				else if(BluetoothControlSettings.DEVICE_TYPE == 1)
					i = new Intent(getApplicationContext(), AdvanceSettingsParani.class);

				if ( i != null )
					startActivityForResult(i, 0);


			}
		});

	}

	public void onItemSelected(AdapterView<?> parent, View view, int pos,
			long id) {

		CharSequence c = (CharSequence) parent.getItemAtPosition(pos);
		Log.d("bluetooth.settings","S-a selectat: " + c);

		if(c.toString().equals("Lego Mindstorm NXT"))
			BluetoothControlSettings.DEVICE_TYPE = 0;
		else if(c.toString().equals("Parani Bluetooth Device"))
			BluetoothControlSettings.DEVICE_TYPE = 1;
		else if(c.toString().equals("Line Follower"))
			BluetoothControlSettings.DEVICE_TYPE = 2;
		
	}

	public void onNothingSelected(AdapterView<?> parent) {


	}

	/**
	 * After exiting the advance settings screen, this method will be called.
	 * It checks to see if the settings are correct ( ex. if Lego Ports are coinciding ).
	 * 
	 */
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {

		Log.d("bluetooth.control.settings", BluetoothControlSettings.integerSettings.toString() );
			
		controlSettings.saveSettings();
		controlSettings.loadSettings();
		
		
	}
	
	@Override
	public void onDestroy()
	{
		super.onDestroy();
		controlSettings.saveSettings();
	}


}
